package com.kezesong.leasing.service.impl;

import com.kezesong.leasing.controller.response.GoodsCategoryListRspBody;
import com.kezesong.leasing.entity.GoodsCategoryEntity;
import com.kezesong.leasing.mapper.GoodsCategoryMapper;
import com.kezesong.leasing.service.GoodsCategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class GoodsCategoryServiceImpl implements GoodsCategoryService {

    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;

    @Override
    public List<GoodsCategoryEntity> getGoodsCategoryList() {
        List<GoodsCategoryEntity> goodsCategoryEntities = goodsCategoryMapper.selectAll();
        return goodsCategoryEntities;
    }

    @Override
    public List<GoodsCategoryListRspBody> getGoodsCategoryShowList() {
        List<GoodsCategoryListRspBody> goodsCategoryListRspBodyArrayList = new ArrayList<>();
        List<GoodsCategoryEntity> goodsCategoryEntities = goodsCategoryMapper.selectAll();
        if (null != goodsCategoryEntities && goodsCategoryEntities.size()>0){
            goodsCategoryEntities.stream().forEach(p->{
                GoodsCategoryListRspBody goodsCategoryListRsp = new GoodsCategoryListRspBody();
                BeanUtils.copyProperties(p, goodsCategoryListRsp);
                goodsCategoryListRspBodyArrayList.add(goodsCategoryListRsp);
            });
        }
        return goodsCategoryListRspBodyArrayList;
    }

    @Override
    public int addGoodsCategory(GoodsCategoryEntity goodsCategoryEntity) {
        goodsCategoryEntity.setCreatedTime(new Date());
        int rows = goodsCategoryMapper.insert(goodsCategoryEntity);
        return rows;
    }

    @Override
    public int modifyGoodsCategory(GoodsCategoryEntity goodsCategoryEntity) {
        goodsCategoryEntity.setUpdatedTime(new Date());
        int rows = goodsCategoryMapper.updateByPrimaryKey(goodsCategoryEntity);
        return rows;
    }

    @Override
    public int deleteGoodsCategory(int categoryId) {
        int rows = goodsCategoryMapper.deleteByPrimaryKey(categoryId);
        return rows;
    }
}
